// pages/wode/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },
  gotodyj(){
    wx.navigateTo({
      url: '/pages/DaYinJi/DaYinJi',
    })
  },
  onShow(){
    var userInfo=wx.getStorageSync('userInfo')
    if(userInfo.storeId){
      this.init()
      console.log('onshow')
      this.storeUserHome()
     
      
    
    }else{
      wx.navigateTo({
        url: '/pages/login/index',
      })
    }
   
  },

  init(){

  },
  storeUserHome(){
    let data = {
      id:wx.getStorageSync('userInfo').storeId
    }
    app.agriknow.storeUserHome(data).then(res=>{
      console.log(res)
      var datas=res.data
      var balance=datas.balance
      var freezePrice=datas.freezePrice
      datas.freezePrice=freezePrice.toFixed(2)
      var withdrawal=datas.withdrawal
      datas.balance=balance.toFixed(2)
      datas.withdrawal=withdrawal.toFixed(2)
      datas.withdrawable=Number(Number(balance)-Number(freezePrice)).toFixed(2)
      this.setData({
        userInfo:datas
      })
      wx.setStorageSync('storeInfo', res.data)
      if(!datas.isApply){
        console.log('未进件')
        wx.reLaunch({
          url: '/pages/wode/merchant',
        })
      }
    })
  },
  gotoMore(){
    wx.navigateTo({
      url: '/pages/withdrawalDetails/index',
    })
  },
  gotobalaWithdrawal(){
    wx.navigateTo({
      url: '/pages/balaWithdrawal/index',
    })
  },
  gotoProductManagent(){
    wx.navigateTo({
      url: '/pages/productManagement/index',
    })
  },
  gotologin(){
    wx.showModal({
      title: '提示',
      content: '是否退出登录',
      success (res) {
        if (res.confirm) {
          wx.setStorageSync('userInfo', '')
          setTimeout(()=>{
            wx.reLaunch({
              url: '/pages/login/index'
            })
          },1000)
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
    
  },
  gotoMy(){
    wx.navigateTo({
      url: '/pages/modifyData/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  toLink:function(e){
      var url=e.currentTarget.dataset.url
      wx.navigateTo({
        url: url,
      })
  },

  scan:function(e) {
    var that=this
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
       
        console.log(res.result); // 扫描结果
       
      that.getOrderInfo(res.result)

      },
      fail(res) {
        console.log(res.errMsg); // 扫描失败的错误信息
      }
    });
  },
  getOrderInfo:function(id){
    
    var that=this
    var data={}
    data.id=id

    app.agriknow.getOrderDetail(data,'ht/user/userQrOrderDetails').then(res=>{
      console.log(res)
      var datas=res.data
      var storeId=wx.getStorageSync('userInfo').storeId
      if(res.code=='000000'){
        console.log(datas.storeId,storeId)
        if(parseInt(datas.storeId)!=parseInt(storeId)){
          wx.showToast({
            title: '非法操作',
            icon:'none',
            duration:1500
          })
          return false
        }
  
        wx.navigateTo({
          url: '/pages/orderDetail/index?id='+datas.id+'&type=1&isScan=1',
        })
      }else{
        wx.showToast({
          title: res.data,
          icon:'none',
          duration:1500
        })
      }
     

    })
   
  },
})